* Housekeeping
set more off
timer clear

* Working paths
global base "/directory"
global DoPth "$base/dofiles/"
global PthIn "$base/data/" 
global PthOut "$base/output/"

* Globals and locals
global starty 1976
global startyplus 1977
global endy 2011

******************************
*** RESULTS FROM MAIN TEXT ***
******************************

*** Table I
use "$PthOut/firm_LBD_master2.dta", clear

gen block2 = 1
replace block2 = 2 if year>1981 & year<1987
replace block2 = 3 if year>1986 & year<1992
replace block2 = 4 if year>1991 & year<1997
replace block2 = 5 if year>1996 & year<2002
replace block2 = 6 if year>2001 & year<2007
replace block2 = 7 if year>2006 & year<2012
replace block2 = . if year==1976 | year>=2012

bys firmid block2: egen stdev_gr = sd(emp_gr_ln2)
bys firmid block2: egen avg_size = mean(emp)

gen lstdev_gr = ln(stdev_gr)
gen lavg_size = ln(avg_size)

bys firmid block2: gen f_obs2 = _n

* Collapse to "firmid-time" block (rather than "firmid-year") level
keep if f_obs2==1 & block2!=.

reghdfe lstdev_gr lavg_size, absorb(block2) vce(cluster fk_naics12_3)
reghdfe lstdev_gr lavg_size, absorb(block2 fk_naics12_3) vce(cluster fk_naics12_3)
reghdfe lstdev_gr lavg_size, absorb(block2 firmid) vce(cluster fk_naics12_3)

*** Figure 1

* "BDSE" (extended BDS)
gen binBDSE2 = 0 
replace binBDSE2 = 1 if avg_size>0 & avg_size<=5 & avg_size!=.
replace binBDSE2 = 2 if avg_size>5 & avg_size<=10 & avg_size!=.
replace binBDSE2 = 3 if avg_size>10 & avg_size<=20 & avg_size!=.
replace binBDSE2 = 4 if avg_size>20 & avg_size<=50 & avg_size!=.
replace binBDSE2 = 5 if avg_size>50 & avg_size<=100 & avg_size!=.
replace binBDSE2 = 6 if avg_size>100 & avg_size<=250 & avg_size!=.
replace binBDSE2 = 7 if avg_size>250 & avg_size<=500 & avg_size!=.
replace binBDSE2 = 8 if avg_size>500 & avg_size<=1000 & avg_size!=.
replace binBDSE2 = 9 if avg_size>1000 & avg_size<=2500 & avg_size!=.
replace binBDSE2 = 10 if avg_size>2500 & avg_size<=5000 & avg_size!=.
replace binBDSE2 = 11 if avg_size>5000 & avg_size<=10000 & avg_size!=.
replace binBDSE2 = 12 if avg_size>10000 & avg_size<=25000 & avg_size!=.
replace binBDSE2 = 13 if avg_size>25000 & avg_size<=50000 & avg_size!=.
replace binBDSE2 = 14 if avg_size>50000 & avg_size<=100000 & avg_size!=.
replace binBDSE2 = 15 if avg_size>100000 & avg_size!=.
replace binBDSE2 = . if avg_size==.

drop if avg_size==0 | avg_size==.

* Extended BDS bins (until 100000 employees)
xi: areg lstdev_gr i.binBDSE2 i.block2, a(firmid) r cluster(fk_naics12_3)
xi: areg lstdev_gr i.binBDSE2 i.block2 [aw=avg_size], a(firmid) r cluster(fk_naics12_3)

